package com.wbdgj.ui.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wbdgj.R;
import com.wbdgj.model.LngLat;
import com.wbdgj.utils.CoodinateCovertor;
import com.wbdgj.utils.ToastUtils;
import com.zhy.autolayout.AutoLayoutActivity;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;


/**
 * Created by Administrator on 2018/3/19.
 */

public class MapDialogAtivity extends Activity {
    private Context context =this;
    private String COORDSX, COORDSY;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.ui_grade_dialog);
        setFinishOnTouchOutside(true);
        initView();
    }

    private void initView(){
        COORDSX = getIntent().getStringExtra("COORDSX");
        COORDSY = getIntent().getStringExtra("COORDSY");

        findViewById(R.id.gdLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isInstallByRead("com.autonavi.minimap")){
                    LngLat lngLat = new LngLat();
                    lngLat.setLantitude(Double.valueOf(COORDSY));
                    lngLat.setLongitude(Double.valueOf(COORDSX));
                    LngLat lngLatgd = CoodinateCovertor.bd_decrypt(lngLat);
                    StringBuffer stringBuffer = new StringBuffer("androidamap://navi?sourceApplication=").append("amap");
                    stringBuffer.append("&lat=").append(lngLatgd.getLantitude())
                            .append("&lon=").append(lngLatgd.getLongitude()).append("&keywords=" )
                            .append("&dev=").append(0)
                            .append("&style=").append(2);
                    Intent mIntent1 = new Intent("android.intent.action.VIEW", Uri.parse(stringBuffer.toString()));
                    mIntent1.setPackage("com.autonavi.minimap");
                    startActivity(mIntent1);
                }else{
                    ToastUtils.toastShort("您没有安装高德地图哦！");
                    Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }

            }
        });

        findViewById(R.id.bdLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isInstallByRead("com.baidu.BaiduMap")){
                    StringBuffer stringBuffer = new StringBuffer("baidumap://map/geocoder?location=")
                            .append(COORDSY).append(",").append(COORDSX).append("&type=TIME");
                    Intent mIntent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(stringBuffer.toString()));
                    mIntent1.setPackage("com.baidu.BaiduMap");
                    startActivity(mIntent1);
                }else{
                    ToastUtils.toastShort("您没有安装百度地图哦！");
                    Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }

            }
        });
    }

    public static boolean isInstallByRead(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }
}
